// 关联函数、关联变量、结构体学习

#[derive(Debug)]
struct Person {
    name: String,
    age: i32
}

#[derive(Debug)]
struct Info {
    sex: String,
    height: f32,
}

#[derive(Debug)]
struct Student {
    base: Person,
    detailed: Info
}

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32
}

#[derive(Debug)]
struct Rectangle {
    top_left: Point,
    bottom_right: Point
}

// 元祖结构体
struct Pair(i32, i64);

enum Flavor {
    Spicy,
    Sweet,
    Fruity,
}

struct Drink {
    flavor: Flavor,
    price: f64,
}

fn square(point: Point, radius: f32) -> Rectangle {
    let bottom_right_x = 1000 / radius as i32;
    let rectangle = Rectangle {
        top_left: point,
        bottom_right: Point {
            x: bottom_right_x,
            y: 20,
        }
    };
    return rectangle;
}

impl Drink {
    // 小写的self指的是Drink结构体里面的内容，外部的内容
    // 大写的Self指的是Drink内部的内容
    // 关联变量
    const MAX_PRICE:f64 = 10.0;

    // 方法
    fn buy(& self) {
        if self.price > Self::MAX_PRICE {
            println!("买不起");
            return;
        }
        println!("买得起");
    }
    // 关联函数
    fn new(price: f64) -> Self {
        Drink {
            flavor: Flavor::Fruity,
            price
        }
    }
}

pub fn struct_study_6() {
    let name = String::from("张三");
    let age = 18;
    let person = Person{ name, age };
    // 打印结构体
    println!("person结构体的内容为：{:?}", person);

    let person1: Person = Person { name: String::from("李四"), age: 24 };
    println!("person1结构体的内容为：{}-{}", person1.name, person1.age);

    let person2 = Person { name: String::from("王五"), age: 64 };
    println!("person2结构体的内容为：{}-{}", person2.name, person2.age);

    // 结构体嵌套
    let student = Student {
        base: Person {
            name: String::from("学生A"),
            age: 18
        },
        detailed: Info {
            sex: String::from("男"),
            height: 161.22,
        }
    };
    println!("student结构体的内容为：{:?}", student);

    let rect_area = Point {
        x: 30,
        y: 40,
    };

    let Point { x, y } = rect_area;
    println!("长方形的面积为: {}", x * y);

    let square1 = square(Point { x, y }, 30.00);
    println!("square1结果为: {:?}", square1);

    let drink = Drink {
        flavor: Flavor::Sweet,
        price: 0.1,
    };

    let sweet = Drink::new(10.001);

    // drink.buy();
    sweet.buy();
}